package com.busionline.wotu.woochenandroid.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *时间转化工具类
 *@author woochen123
 *@time 2017/3/21 15:35
 */
public class TimeUtil {

    /**
     *得到阶段性日期
     * @param monthBefore  几月前
     * @param dayBefore 几天前
     * @return “2011-4-10”
     */
    public static String getBeforeDate(int monthBefore, int dayBefore){
        Calendar c = Calendar.getInstance();
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH) - monthBefore, c.get(Calendar.DAY_OF_MONTH) - dayBefore);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return year + "-" + (month + 1) + "-" + day;
    }

    /**
     * 得到当前日前
     * @return  “2011-4-10”
     */
    public static String getCurrentDate(){
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return year + "-" + (month + 1) + "-" + day;
    }



    /**
     * 时间戳转化为字符串
     * @param time unix时间戳 单位毫秒
     * @return “2012-2-14 5:12:20”
     */
    public static String parseTime2(long time) {
        if (time == 0 ){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stdTime = sdf.format(new Date(time * 1000L));
        return stdTime;
    }

    /**
     * 字符串转时间戳
     * @param timeString
     * @return  "1490025600"
     */
    public static String getTimeStamp(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = sdf.parse(timeString);
            long tempTimeStamp = d.getTime();
            timeStamp = String.valueOf(tempTimeStamp);
            if (timeStamp.length() == 13) {
                //默认转化出来的时间戳是13位，需要变成10位
                timeStamp = String.valueOf(tempTimeStamp / 1000);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }


    /**
     * 根据格式返回当前日期
     * @param dateFormatStr "yyyy-MM-dd HH：mm"
     * @return "2012-02-11 12:12"
     **/
    private String getAssignTime(String dateFormatStr) {
        DateFormat dataFormat = new SimpleDateFormat(dateFormatStr);
        long currentTime = System.currentTimeMillis();
        return dataFormat.format(currentTime);
    }


    /**
     * 计算目标时间与当前时间的间隔
     * @param publishTime
     * @desc 如：3天前
     * @return
     */
    public static String parseTime(long publishTime) {
        long time;
        time = (System.currentTimeMillis() - publishTime) / 1000;
        int day, hour, minute, senond;
        day = (int) (time / 3600 / 24);

        if (day == 1)
            return "1天前";
        if (day > 1)
            return new SimpleDateFormat("yyyy-MM-dd").format(publishTime);
        hour = (int) (time / 3600);
        if (hour > 0)
            return String.valueOf(hour) + "小时前";
        minute = (int) (time / 60);
        if (minute > 0)
            return String.valueOf(minute) + "分钟前";
        senond = (int) time;
        if (senond >= 0)
            return "刚刚";
        return "";
    }

}
